<?php

namespace app\api\controller\xykeep;
use app\common\controller\Api;

use app\api\model\xykeep\Take as TakeModel;
use app\api\model\xykeep\Goods as GoodsModel;
use app\api\model\xykeep\goods\Order as GoodsOrderModel;
use app\api\model\xykeep\Category as CategoryModel;
use app\api\model\xykeep\goods\Activity as ActivityModel;
use think\Db;

/**
 * XYkeep商品接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['lists','detail','categories','banner'];
    protected $noNeedRight = ['*'];
    
	/**
	 * 商品列表
	 */
	public function lists()
    {
    	$params = $this->request->get();
        $data = GoodsModel::getList($params);
        $this->success('商品列表', $data);
    }

    /**
     * 查询订单金额
     */
    public function countPrice()
    {
        $params = $this->request->get();
        $detail = GoodsModel::getDetail($params['id']);
        $data = GoodsOrderModel::count_price($params['nums'],$detail['price'],$detail['id']);
        $this->success('计算价格', $data);
    }

    /**
     * 商品详情
     */
    public function detail()
    {
        $id = $this->request->get('id');
        $detail = GoodsModel::getDetail($id);

        if(!$detail){
            $this->error('商品不存在！');
        }

        $sku_price = $detail['sku_price'];
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;

        $this->success('商品详情', $detail);
    }

    /**
     * 商品分类
     */
    public function categories(){
        $params = $this->request->get();
        $data = CategoryModel::getAllCategory($params);
        $this->success('商品分类', $data);
    }

    /**
     * 商城banner
     */
    public function banner()
    {
        $banner = Db::name('xykeep_goods_banner')->where(['status'=>'normal'])->select();
        $this->success('banner', $banner);
    }

    /**
     * 每周一花
     */
    public function activitys(){
        $params = $this->request->get();
        $data = ActivityModel::select();
        foreach ($data as $val){
            $val['goods'] = GoodsModel::getList(['activity_id'=>$val['id']]);
        }
        $this->success('每周一花', $data);
    }

    //自提点列表
    public function take_list()
    {
        $take = TakeModel::select();
        $this->success('take', $take);
    }
	
}